package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.CheckGroupService;
import com.itheima.health.service.CheckItemService;
import com.itheima.health.service.SetMealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ：石破天
 * @date ：Created in 2022年06月07日
 * @description ：
 * @version: 1.0
 */
@RestController
@RequestMapping("/mobile/setmeal")
@Slf4j
public class MobileSetMealController {
    @Autowired
    private SetMealService setMealService;
    @Autowired
    private CheckGroupService checkGroupService;
    @Autowired
    private CheckItemService checkItemService;


    /*
    *根据id获取套餐信息
    * */
    @Transactional
    @GetMapping("/findById")
    public Result getSetmeal(Integer id){
        Setmeal setmeal = setMealService.getSetmeal(id);

        //查询套餐关联表中所有checkgroupid  通过在checkgroup表查询出list
        List<CheckGroup> checkGroups = setMealService.findCheckGroupIdsBySetmealId(id);

        //查询检查组关联表中所有item项id    通过在checkItem表中查询出list放进checkgroupdto
        for (CheckGroup checkGroup : checkGroups) {
            //查询当前checkGroup下的所有检查项id
            List<Integer> checkItemIds = checkGroupService.findCheckItemIdsByCheckGroupId(checkGroup.getId());
            List<CheckItem> checkItems = new ArrayList<>();
            for (Integer checkItemId : checkItemIds) {
                //遍历检查项id 找出检查项 添加到本次的列表中
                CheckItem checkItem = checkItemService.findById(checkItemId);
                checkItems.add(checkItem);
            }
            checkGroup.setCheckItems(checkItems);
        }
        setmeal.setCheckGroups(checkGroups);
        return new Result(true,MessageConst.QUERY_SETMEAL_SUCCESS,setmeal);
    }

    /**
     *查询套餐展示列表
     */
    @GetMapping("/getSetmeal")
    public Result getSetMeal() {
         List<Setmeal> setmealList=setMealService.findSetmealList();
        return new Result(true, MessageConst.GET_SETMEAL_LIST_SUCCESS, setmealList);

    }

}
